<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <DialogTable
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="api"
      :listen-height="false"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="true"
      :check-box="false"
      @handleEvent="handleEvent"
    />
  </page-dialog>
</template>

<script setup name="selectLicense">
import PageDialog from '../PageDialog/index'
import DialogTable from '../DialogTable/index'
import { getLicensePage } from '@/api/core.js'
import PageFilter from '../PageFilter/index'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['update', 'callback', 'reset'])
const props = defineProps({
  showDialog: {
    type: Boolean,
  },
  api: {
    type: Function,
    default: getLicensePage,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
})

// 过滤相关配置
const filterInfo = reactive({
  query: {
    nameLike: '',
    sort: 'create_time desc',
    ...props.query,
  },
  list: [
    {
      type: 'input',
      label: '名称',
      value: 'licenseNameLike',
      event: search,
    },
    {
      type: 'input',
      label: '证号',
      value: 'licenseNumberLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '证号', value: 'licenseNumber' },
    { label: '证照类型', value: 'licenseTypeValue' },
    { label: '证照分类', value: 'licenseApplicationTypeValue' },
    { label: '产品名称', value: 'licenseName' },
    { label: '生效日期', value: 'licenseBeginDate' },
    { label: '失效日期', value: 'licenseEndDate' },
    { label: '厂家名称', value: 'brandName' },
    { label: '状态', value: 'statusValue' },
    { label: '备注', value: 'remark' },
  ],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '选择证照',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

let shows = computed({
  get() {
    console.log(props.showDialog)
    return props.showDialog
  },
  set(newValue) {
    console.log(newValue)
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})
onMounted(() => {
  proxy.$nextTick(() => {
    getList()
  })
})
// 获取列表
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function save() {
  // emit('input', data)
  if (selectRow.value) {
    handleClose()
    emit('callback', selectRow.value)
  } else {
    handleClose()
    emit('callback', selectRow.value)
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emit('reset')
}
</script>

<style scoped>
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
